<?php

namespace App\Model\Account;

use App\Core\BaseModel;
use App\Model\Member\MemberModel;
use EasySwoole\Component\Di;
use EasySwoole\Redis\Redis;

class RechargeModel extends BaseModel
{

    protected $tableName = 'ss_recharge';
    protected static $logTableName = 'ss_recharge_log';

    protected $casts = [
        'id'              => 'integer',
        'user_id'         => 'integer',
        'amout'           => 'integer',
        'score'           => 'integer',
        'recharge_state'  => 'string',
        'payment_type'    => 'string',
        'payment_state'   => 'string',
        'payment_content' => 'string',
        'remark'          => 'string'
    ];

    //充值状态 INPROGRESS:进行中,FINISHED:已完成,CANCELLED:已取消
    const INPROGRESS = 'INPROGRESS';//进行中
    const FINISHED   = 'FINISHED';//已完成
    const CANCELLED  = 'CANCELLED';//已取消

    //充值方式: USDT:usdt,WECHAT:微信,ALIPAY:支付宝,PUB_BANK:对公,PRI_BANK:对私
    const PAYMENT_USDT     = 'USDT';//usdt
    const PAYMENT_WECHAT   = 'WECHAT';//微信
    const PAYMENT_ALIPAY   = 'ALIPAY';//支付宝
    const PAYMENT_PUB_BANK = 'PUB_BANK';//对公
    const PAYMENT_PRI_BANK = 'PRI_BANK';//对私
    const PAYMENT_BINANCE = 'BINANCE';//币安
    const PAYMENT_TRX = 'TRX';//波场

    //支付状态:UNPAID:未支付, PAID:已支付
    const UNPAID = 'UNPAID';//未支付
    const PAID   = 'PAID';//已支付

    public static function getRechargeStateDict($key = null)
    {
        return self::getMap([
            self::INPROGRESS => '审核中',
            self::FINISHED   => '已完成',
            self::CANCELLED  => '已取消',
        ], $key);
    }

    public static function getPaymentTypeDict($key = null)
    {
        return self::getMap([
//            self::PAYMENT_USDT     => 'USDT',
            self::PAYMENT_BINANCE => '币安',
            self::PAYMENT_TRX => '波场',
//            self::PAYMENT_WECHAT   => '微信',
//            self::PAYMENT_ALIPAY   => '支付宝',
//            self::PAYMENT_PUB_BANK => '对公转账',
//            self::PAYMENT_PRI_BANK => '对私转账',
        ], $key);
    }

    public static function getPaymentStateDict($key = null)
    {
        return self::getMap([
            self::UNPAID => '未支付',
            self::PAID   => '已支付',
        ], $key);
    }

    public function member()
    {
        return $this->hasOne(MemberModel::class, function ($query) {
            $query->where('is_delete', 0);
            return $query;
        }, 'user_id', 'id');
    }
    protected static function onAfterUpdate($model, $res)
    {
        self::delCache('rechargeCache:');
    }

    protected static function onAfterInsert($model, $res)
    {
        self::delCache('rechargeCache:');
    }
}
